﻿#include "importhpgl.h"
#include <QFile>
#include <qmath.h>
#include <QDebug>
#include "vectorfont.h"
#include "main.h"

ImportHPGL::ImportHPGL(QObject *parent) :
    QObject(parent)
{
    m_iDPMM = 40;
    m_iPenNo = 1;
    m_ptCurrentPos = QPoint(0,0);
    m_lineType.bDefault = true;
    m_bPenUp = true;

    m_pMarker = new Marker();
    m_dScale = (double)1.0;

    m_chTerminator = 3;
    m_nTerminatorMode = 1;

    m_listXY.clear();

    m_penPen.setColor(QColor(0,0,0));
    m_cutPen.setColor(QColor(0,255,0));
    m_halfCutPen.setColor(QColor(0,0,255));

    m_dTextHeight = 0.375 * 10 * m_iDPMM;//字的高度
    m_dTextWidth = 0.285 * 10 * m_iDPMM;//字的宽度
    m_nLableOrigin = 1;//字符串相对于原点位置
    m_dTextAngle = 0;//指定每行文本输出时相对于设备x轴的角度，其单位为1/10度

    m_dScaleX = 1;
    m_dScaleY = 1;
    m_ptOrigin = QPoint(0,0);
    m_nLength = 0;
    m_bPage = false;
    m_bFontItalic = false;
    m_iFontSize = 0;
    m_strFontName = "";

    m_pFileBuf = NULL;

    m_embAbsData.clear();
    m_maxX = S32_MIN;
    m_maxY = S32_MIN;
    m_minY = S32_MAX;
    m_minX = S32_MAX;
    m_stepCtrl = DATA_SEWING;
}

ImportHPGL::~ImportHPGL()
{
    if(m_pMarker != NULL)
    {
        delete m_pMarker;
        m_pMarker = NULL;
    }

    if (m_pFileBuf != NULL)
    {
        delete []m_pFileBuf;
        m_pFileBuf = NULL;
    }
}

int ImportHPGL::GetQRVesion(int nCount)
{
    int nVesion = 1;

    if (nCount < 17)
    {
        nVesion  = 1;
    }
    else if ((nCount >= 17) && (nCount < 32))
    {
        nVesion  = 2;
    }
    else if ((nCount >= 32) && (nCount < 53))
    {
        nVesion  = 3;
    }
    else if ((nCount >= 53) && (nCount < 78))
    {
        nVesion  = 4;
    }
    else if ((nCount >= 78) && (nCount < 106))
    {
        nVesion  = 5;
    }
    else if ((nCount >= 106) && (nCount < 134))
    {
        nVesion  = 6;
    }
    else if ((nCount >= 134) && (nCount < 154))
    {
        nVesion  = 7;
    }
    else if ((nCount >= 154) && (nCount < 192))
    {
        nVesion  = 8;
    }
    else if ((nCount >= 192) && (nCount < 230))
    {
        nVesion  = 9;
    }
    else if ((nCount >= 230) && (nCount < 271))
    {
        nVesion  = 10;
    }
    else
    {
        nVesion  = 40;
    }
    return nVesion;
}

void ImportHPGL::PointRotate(QPoint &ptPoint, QPoint ptPointO, double dSinBeta, double dCosBeta)
{
    int tx = ptPoint.x() - ptPointO.x();
    int ty = ptPoint.y() - ptPointO.y();

    int nx = tx*dCosBeta - ty*dSinBeta;
    int ny = tx*dSinBeta + ty*dCosBeta;

    ptPoint.setX(nx);
    ptPoint.setY(ny);
}

void ImportHPGL::IniPara()
{
    m_fileFullPathName.clear();
    m_iPenNo = 1;//当前的笔号
    m_ptCurrentPos = QPoint(0,0);//笔的当前位置,单位:(1/CMarker::m_iDPMM)毫米
    m_lineType.bDefault = true;//线的类型是否设置值
    m_bPenUp = true;//当前刀具的状态 true:刀具状态为抬起  false：刀具状态为落下

    m_pMarker->Initialize();
    m_dScale=(double)1.0;

    m_chTerminator = 3;//标志终结符,除了NULL LF ESC及;十进制分别为0,5,27,59;外的所有字符，默认为ETX 十进制3
    m_nTerminatorMode = 1;//模式0：打印1：不打印 默认值为1

    m_listXY.clear();

    m_dTextHeight = 0.375 * 10 * m_iDPMM;//字的高度
    m_dTextWidth = 0.285 * 10 * m_iDPMM;//字的宽度
    m_nLableOrigin = 1;//字符串相对于原点位置
    m_dTextAngle = 0;//指定每行文本输出时相对于设备x轴的角度，其单位为1/10度

    m_dScaleX = 1;//通过 IP和SC指令算出的缩放比例
    m_dScaleY = 1;//通过 IP和SC指令算出的缩放比例
    m_ptOrigin = QPoint(0,0);//通过 IP和SC指令算出的原点
    m_nLength = 0;//用于PG分页，记录累计长度
    m_bPage = false;//是否识别到PG指令
    m_lineType.bDefault = true;//线的类型是否设置值
}

bool ImportHPGL::Read(QString strPathName)
{
    m_fileFullPathName = strPathName;
    QFile file(strPathName);
    char c;
    bool bOk;

    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug() <<"file open failed";
        return false;
    }

    m_dScale=(double)m_pMarker->m_iDPMM / m_iDPMM;
    m_listXY.clear();
    m_nFileLength = file.size();
    m_pFileBuf = new char[m_nFileLength];
    file.read(m_pFileBuf,m_nFileLength);
    file.close();
    m_nCharCount = 0;
    bOk=GetChar(&c);
    while (bOk && m_nCharCount < m_nFileLength)
    {
        switch (c)
        {
        case 'S':  //S命令
        case 's':
            bOk=S_Code();
            break;
        case 'I':  //I命令
        case 'i':
            bOk=I_Code();
            break;
        case 'P':  //P命令
        case 'p':
            bOk=P_Code();
            break;
        case 'L':  //L命令
        case 'l':
            bOk=L_Code();
            break;
        case 'D':
        case 'd':
            bOk=D_Code();
            break;
        case 'C':
        case 'c':
            bOk=C_Code();
            break;
        case ';':
        case ' ':
        case 0x0A://换行
        case 0x0D://回车
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }

        if (bOk)
        {
            bOk=GetChar(&c);
            if (m_nCharCount == m_nFileLength)  bOk=true;
        }
    }

    AddPolylineToMarker();

    if (m_pFileBuf != NULL)
    {
        delete []m_pFileBuf;
        m_pFileBuf = NULL;
    }

    return bOk;
}

#if(0)
void ImportHPGL::creatPolylinePainterPath()
{
    if(m_pMarker == NULL){return;}

    QPainterPath painterPath;
    QRect rect = m_pMarker->GetRect();
    int minX = rect.left();
    int minY = rect.top();
    int maxY = rect.bottom();

    int nLineCount = m_pMarker->m_listPolyline.size();
    //    m_polyTextPath = QPainterPath();
    for(int i = 0; i < nLineCount; i++)
    {
        CRPPolyline polyLine = m_pMarker->m_listPolyline.at(i);
        int type = polyLine.m_nDrawingType;
        //        if(polyLine.m_nDrawingType == 3)//文字
        //        {
        //            CRPText text = polyLine.m_text;
        //        }

        int nPointCount = polyLine.m_listPoint.size();
        for(int j = 0; j < nPointCount; j++)
        {
            double x = (polyLine.m_listPoint.at(j).x() - minX)/(double)M_IDPMM*MMPIXELY;
            double y = ((0 - (polyLine.m_listPoint.at(j).y() - minY))+(maxY-minY))/(double)M_IDPMM*MMPIXELY;
            QPointF point(x,y);

            if(j == 0)
            {
                painterPath.moveTo(point);
            }
            else
            {
                painterPath.lineTo(point);
            }
        }
    }

    m_polylinePainterPath = painterPath;
}
#endif

//读取一个非空格、非回车、非换行的字符
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  *pChar  读取的字符
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功取得一个字符
//  false 失败
bool ImportHPGL::GetChar(char *pChar)
{
    char c;
    uint nCount;
    bool bOk;
    bOk=true;

    nCount = ReadChar(&c);
    while ((nCount == 1) && ((c == ' ') || (c == 0x0A) || (c == 0x0D)))
    {
        nCount = ReadChar(&c);
    }

    if (bOk && (nCount == 0))
    {
        bOk=false;
    }

    if (nCount == 1)
    {
        *pChar=c;
        bOk=true;
    }

    return bOk;
}

uint ImportHPGL::ReadChar(char* lpBuf)
{
    uint nCount = 1;

    if (m_nCharCount < m_nFileLength)
    {
        *lpBuf = m_pFileBuf[m_nCharCount];
        m_nCharCount = m_nCharCount + 1;
        nCount = 1;
    }
    else
    {
        nCount = 0;
    }

    return nCount;
}

//判断下一个非空格、非回车、非换行的字符是不是','
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 下一个非空格、非回车、非换行的字符是',', 并且已将','从文件中读出
//  false 下一个非空格、非回车、非换行的字符不是',', 并且该字符没有从文件中读出
bool ImportHPGL::NextCharIsComma()
{
    char cCh1;
    bool bNextCharIsStart;

    bNextCharIsStart=false;

    if (GetChar(&cCh1))
    {
        if (cCh1 == ',')
        {
            bNextCharIsStart=true;
        }
        else
        {
            m_nCharCount = m_nCharCount - 1;
        }
    }

    return bNextCharIsStart;
}

//取一个整数
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  iValue  取得的整数
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功取得一个整数iValue
//  false 失败
bool ImportHPGL::GetIntegerData(int &iValue)
{
    QString string1;
    char c;
    bool bOk,bNum;

    bNum=false;

    bOk=GetChar(&c);
    if (bOk && (c == '-'))
    {
        string1=string1 + c;
        bOk=GetChar(&c);
    }
    while (bOk)
    {
        if (('0' <= c) && (c <= '9'))
        {
            string1=string1 + c;
            bNum=true;
        }
        else
        {
            m_nCharCount = m_nCharCount - 1;
            break;
        }

        bOk=GetChar(&c);
    }

    if (!bOk)
    {
        if (m_nCharCount == m_nFileLength)
            bOk=true;
    }
    if (!bNum)
        bOk=false;

    if (bOk)
        iValue=string1.toInt();

    return bOk;
}

//取一个双精度浮点数
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  dValue  取得的双精度浮点数
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功取得一个双精度浮点数dValue
//  false 失败
bool ImportHPGL::GetDoubleData(double &dValue)
{
    QString string1;
    char c;
    bool bOk,bNum;

    bNum=false;

    bOk=GetChar(&c);
    if (bOk && (c == '-'))
    {
        string1=string1 + c;
        bOk=GetChar(&c);
    }
    while (bOk)
    {
        if ((('0' <= c) && (c <= '9')) || c == '.')
        {
            string1=string1 + c;
            bNum=true;
        }
        else
        {
            m_nCharCount = m_nCharCount - 1;
            break;
        }

        bOk=GetChar(&c);
    }

    if (!bOk)
    {
        if (m_nCharCount == m_nFileLength)
            bOk=true;
    }

    if (!bNum)
        bOk=false;

    if (bOk)
        dValue=string1.toDouble();

    return bOk;
}

//取坐标值
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  ptPoint  坐标
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功
//  false 失败
bool ImportHPGL::GetCoordinate(QPoint &ptPoint)
{
    bool bOk;
    int iX,iY;
    double x,y;
    QString strInfo;

    ptPoint=m_ptCurrentPos;
    //bOk=GetIntegerData(x);
    bOk=GetDoubleData(x);//为了兼容Gemini文件，Gemini坐标不是整数，是小数
    if (bOk)
    {
        iX=(double)(x+m_nLength) * m_dScale * m_dScaleX;
        ptPoint.setX(iX);
    }

    if (bOk)
    {
        bOk=SearchChar(',' ,strInfo);
        if (bOk)
        {
            //bOk=GetIntegerData(y);
            bOk=GetDoubleData(y);//为了兼容Gemini文件，Gemini坐标不是整数，是小数
            if (bOk)
            {
                iY=(double)y * m_dScale * m_dScaleY;
                ptPoint.setY(iY);
            }

        }
    }

    return bOk;
}

//获得线的类型
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  lineType  线型
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功
//  false 失败
bool ImportHPGL::GetLineType(LineType &lineType)
{
    bool bOk;
    int nLineType, nPatternLength, nMode;
    QString strInfo;
    lineType  = m_lineType;
    bOk=GetIntegerData(nLineType);
    if (bOk)
    {
        lineType.bDefault = false;
        lineType.nLinetype = nLineType;
        bOk=SearchChar( ',' ,strInfo);
        if (bOk)
        {
            bOk=GetIntegerData(nPatternLength);
            if (bOk)
            {
                lineType.nPatternLength = nPatternLength;
            }

            bOk=SearchChar( ';' ,strInfo);
            if (bOk)
            {
                bOk=GetIntegerData(nMode);
                if (bOk)
                {
                    lineType.nMode = nMode;
                }
            }
            else
            {
                lineType.nMode = 0;
            }
        }
        else
        {
            lineType.nPatternLength = 4;
        }
    }
    else
    {
        lineType.bDefault = true;
    }

    return bOk;
}

//查找一个指定的字符
//输入参数:
//  pFile 切割数据文件
//  cFind 指定的字符
//输出参数:
//  bEndOfFile =true 已到文件尾
//  strInfo 从开始位置到指定字符之前的内容(不包括最后的指定字符)
//返回值:
//  true 成功取得一个字符
//  false 失败
bool ImportHPGL::SearchChar(char cFind,QString &strInfo)
{
    char c;
    uint nCount;
    bool bOk;

    bOk=true;
    strInfo.clear();


    nCount = ReadChar(&c);
    while ((nCount == 1) && (c != cFind))
    {
        strInfo = strInfo + c;
        nCount = ReadChar(&c);
    }


    if (bOk && (nCount == 0))
    {
        bOk=false;
    }

    if (nCount == 1)
    {
        bOk=true;
    }

    return bOk;
}

//将文件的读取位置移到下一个英文字母(26个英文字母)
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功将文件的读取位置移到下一个英文字母
//  false 失败
bool ImportHPGL::MoveToNextEnglishChar()
{
    char c;
    uint nCount;
    bool bOk, bFind;

    bOk=true;

    bFind = false;
    nCount = ReadChar(&c);
    while ((nCount == 1) && (!bFind))
    {
        if ((('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z')))
        {
            m_nCharCount = m_nCharCount - 1;
            bFind = true;
        }
        else
        {
            nCount = ReadChar(&c);
        }
    }

    return bOk;
}

//将m_listXY加入到m_pMarker中,并将m_listXY清空
void ImportHPGL::AddPolylineToMarker()
{
    //长度<0.2mm为钻孔
    CRPPolyline RPPolyline1;
    QPoint pt1,pt2;
    CDrill drill;
    bool bIsdirll = false;

    if (m_listXY.size() >= 2)
    {
        if (m_listXY.size() == 2 && m_iPenNo == 3)
        {
            pt1 = m_listXY.at(0);
            pt2 = m_listXY.at(1);
            if (((pt1.x() - pt2.x())*(pt1.x() - pt2.x())+(pt1.y() - pt2.y())*(pt1.y() - pt2.y()))<=64)
            {
                bIsdirll = true;
            }
        }
        if (bIsdirll)
        {
            drill.m_pt = pt1;
            drill.m_nDrillType = 0;
            drill.m_nAngle = 0;
            RPPolyline1.m_nDrawingType = 5;
            RPPolyline1.m_drill = drill;
            RPPolyline1.m_nPenNo = drill.m_nDrillType;
        }
        else
        {
            RPPolyline1.m_nPenNo = m_iPenNo;
            RPPolyline1.m_bPgEnd = m_bPage;
            RPPolyline1.m_lineType = m_lineType;
            RPPolyline1.m_listPoint.clear();
            RPPolyline1.m_listPoint.append(m_listXY);
        }
        m_pMarker->m_listPolyline.append(RPPolyline1);
    }
    else
    {
        if (m_iPenNo == 20 && m_listXY.size() > 0)
        {
            drill.m_pt = m_listXY.at(0);
            drill.m_nDrillType = 0;
            drill.m_nAngle = 0;
            RPPolyline1.m_nDrawingType = 5;
            RPPolyline1.m_drill = drill;
            RPPolyline1.m_nPenNo = drill.m_nDrillType;
            m_pMarker->m_listPolyline.append(RPPolyline1);
        }
    }
    m_listXY.clear();
}

void ImportHPGL::AddPoint2listXY(QPoint ptPoint)
{
    if ((m_listXY.size() == 0) && (m_ptCurrentPos != ptPoint))
    {
        m_listXY.append(m_ptCurrentPos);
    }

    m_listXY.append(ptPoint);
}

void ImportHPGL::AddTextPointToLine(bool bUp,QPoint ptPoint)
{
    if (bUp)
    {
        AddPolylineToMarker();
        m_ptCurrentPos = ptPoint;//笔的当前位置,单位:(1/CMarker::m_iDPMM)毫米
        m_bPenUp = true;
    }
    else
    {
        AddPoint2listXY(ptPoint);
        m_ptCurrentPos = ptPoint;
        m_bPenUp = false;
    }
}

QPoint ImportHPGL::GetTextOrigin(QPoint pt,QString strText)
{
    int nLen;
    double dTextAngle;
    double dLength;
    int iX;
    int iY;
    nLen = strText.size();
    dTextAngle = m_dTextAngle/180.0*PI ;
    switch(m_nLableOrigin)
    {
    case 1:
        iX=pt.x()-(m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(m_dTextHeight)*qCos(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 2:
        iX=pt.x()-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 4:
        iX=pt.x()-(m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 5:
        iX=pt.x()-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 6:
        iX=pt.x()-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=pt.y()-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 7:
        iX=pt.x()-(m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 8:
        iX=pt.x()-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 9:
        iX=pt.x()-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=pt.y()-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 11:
        dTextAngle=qAtan2(0.25*m_dTextHeight,0.25*m_dTextWidth)+m_dTextAngle;
        dTextAngle=dTextAngle/180.0*PI ;
        dLength=qSqrt((0.25*m_dTextHeight)*(0.25*m_dTextHeight)+(0.25*m_dTextWidth)*(0.25*m_dTextWidth));
        iX=pt.x()+dLength*qCos(dTextAngle);
        iY=pt.y()+dLength*qSin(dTextAngle);
        dTextAngle=m_dTextAngle/180.0*PI;
        iX=iX-(m_dTextHeight)*qSin(dTextAngle);
        iY=iY+(m_dTextHeight)*qCos(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 12:
        iX=pt.x()+(0.25*m_dTextWidth)*qCos(dTextAngle);
        iY=pt.y()+(0.25*m_dTextWidth)*qSin(dTextAngle);
        iX=iX-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=iY+(0.5*m_dTextHeight)*qCos(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 13:
        dTextAngle=qAtan2(0.25*m_dTextHeight,0.25*m_dTextWidth)-m_dTextAngle;
        dTextAngle=dTextAngle/180.0*PI;
        dLength=qSqrt((0.25*m_dTextHeight)*(0.25*m_dTextHeight)+(0.25*m_dTextWidth)*(0.25*m_dTextWidth));
        iX=pt.x()+dLength*qCos(dTextAngle);
        iY=pt.y()-dLength*qSin(dTextAngle);
        dTextAngle=m_dTextAngle/180.0*PI;
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 14:
        iX=pt.x()-(1.25*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(1.25*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 15:
        iX=pt.x()-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 16:
        iX=pt.x()+(0.25*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()-(0.25*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 17:
        dTextAngle=qAtan2(0.25*m_dTextHeight,0.25*m_dTextWidth)-m_dTextAngle;
        dTextAngle=dTextAngle/180.0*PI;
        dLength=qSqrt((0.25*m_dTextHeight)*(0.25*m_dTextHeight)+(0.25*m_dTextWidth)*(0.25*m_dTextWidth));
        iX=pt.x()-dLength*qCos(dTextAngle);
        iY=pt.y()+dLength*qSin(dTextAngle);
        dTextAngle=m_dTextAngle/180.0*PI;
        iX=iX-(m_dTextHeight)*qSin(dTextAngle);
        iY=iY+(m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 18:
        iX=pt.x()-(0.25*m_dTextWidth)*qCos(dTextAngle);
        iY=pt.y()-(0.25*m_dTextWidth)*qSin(dTextAngle);
        iX=iX-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 19:
        dTextAngle=qAtan2(0.25*m_dTextHeight,0.25*m_dTextWidth)+m_dTextAngle;
        dTextAngle=dTextAngle/180.0*PI;
        dLength=sqrt((0.25*m_dTextHeight)*(0.25*m_dTextHeight)+(0.25*m_dTextWidth)*(0.25*m_dTextWidth));
        iX=pt.x()-dLength*qCos(dTextAngle);
        iY=pt.y()-dLength*qSin(dTextAngle);
        dTextAngle=m_dTextAngle/180.0*PI;
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    }
    return pt;
}

double ImportHPGL::GetTextAngle(double dX,double dY)
{
    double dAngle = 0;

    if ((dX == 0) && (dY > 0))
    {
        dAngle = 90;
    }
    else if ((dX == 0) && (dY < 0))
    {
        dAngle = -90;
    }
    else if ((dX > 0) && (dY == 0))
    {
        dAngle = 0;
    }
    else if ((dX < 0) && (dY == 0))
    {
        dAngle = 180;
    }
    else if ((dX!=0) && (dY!=0))
    {
        dAngle=qAtan2(dY,dX)*180/PI;
    }

    double tmp = (dAngle - (int)dAngle);
    if(qAbs((int)(tmp*10)) >= 5)
    {
        double angle = dAngle < 0 ? dAngle += -1 : dAngle+=1;
        dAngle = angle;
    }
    dAngle = (int)dAngle;

    return dAngle;
}

void ImportHPGL::SetScale()
{
    double dScaleX,dScaleY;
    switch(m_sc.nType)
    {
    case 0://异步
        m_dScaleX=(double)(m_ptP2.x()-m_ptP1.x())/(m_sc.dXMax-m_sc.dXMin);
        m_dScaleY=(double)(m_ptP2.y()-m_ptP1.y())/(m_sc.dYMax-m_sc.dYMin);
        m_ptOrigin.setX(m_ptP1.x()-m_sc.dXMin*m_dScaleX);
        m_ptOrigin.setY(m_ptP1.y()-m_sc.dYMin*m_dScaleY);
        break;
    case 1://同步
        dScaleX=(double)(m_ptP2.x()-m_ptP1.x())/(m_sc.dXMax-m_sc.dXMin);
        dScaleY=(double)(m_ptP2.y()-m_ptP1.y())/(m_sc.dYMax-m_sc.dYMin);
        if(dScaleX>dScaleY) //x>y
        {
            m_dScaleX=dScaleY;
            m_dScaleY=dScaleY;
            m_ptOrigin.setY(m_ptP1.y());
            m_ptOrigin.setX(((m_ptP2.x()-m_ptP1.x())-(m_sc.dXMax-m_sc.dXMin)*dScaleY)*m_sc.dLeft/100.0);
        }
        else //x<y
        {
            m_dScaleX=dScaleX;
            m_dScaleY=dScaleX;
            m_ptOrigin.setX(m_ptP1.x());
            m_ptOrigin.setY(((m_ptP2.y()-m_ptP1.y())-(m_sc.dYMax-m_sc.dYMin)*dScaleX)*m_sc.dBottom/100.0);
        }
        break;
    case 2://点因子
        m_dScaleX=m_sc.dXMax;
        m_dScaleY=m_sc.dYMax;
        m_ptOrigin.setX(m_ptP1.x()-m_sc.dXMin*m_dScaleX);
        m_ptOrigin.setY(m_ptP1.y()-m_sc.dYMin*m_dScaleY);
        break;
    }
}

bool ImportHPGL::IsSecretFile(QString strPatnName)
{
    QFile file(strPatnName);
    if (!file.open(QIODevice::ReadOnly))
    {
        return false;
    }
    int nLength = file.size();
    if (nLength >= 22)
    {
        char cFileBuf[23];
        file.read(&cFileBuf[0],22);
        if((cFileBuf[0] == 0x66) && (cFileBuf[1] == 0x53) &&(strncmp(&cFileBuf[3],"Richpeace plot file",19) == 0))
        {
            file.close();
            return true;
        }
        else
        {
            file.close();
            return false;
        }
    }
    return false;
}

int ImportHPGL::BitMapDtat(QString strFileName, QString strSecretFile)
{
    int i,j,k,m,n,nRemainChar;
    char cFileBuf[1025];
    char cSecretBuf[256];
    char errRe;
    QFile file(strFileName);  // 当前选择花样路径
    int nLength;
    BOOL bError = FALSE;

    m=j=0;
    nRemainChar = 0;
    errRe=0;
    QFile hFile(strSecretFile);

    if (!hFile.open(QIODevice::WriteOnly|QIODevice::Truncate))
    {
        return 0;
    }

    if(!file.open(QIODevice::ReadOnly))
    {
        return 0;
    }

    nLength = file.size(); // 文件长度790
    file.read(&cFileBuf[0],22);
    i = 22;

    for(;i<nLength;)
    {
        k = 0;
        if (m < j)
        {
            while(k<(j-m))
            {
                cFileBuf[k] = cFileBuf[m+k];
                k++;
            }
            j = 1024 - k;
        }
        else
            j = 1024;
        if (nRemainChar > 0)
        {
            n = 252 - nRemainChar + 2;
            if (j > nRemainChar)
            {
                m = 254;
                j -= nRemainChar;
                i += nRemainChar;
            }
            else
            {
                m = n + j;
                i += j;
                j = 0;
            }
            if ((n < m) && ((nRemainChar & 0x01) != 0))
            {
                nRemainChar -= (m - n);
                cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[1];
            }
            else
                nRemainChar -= (m - n);
            for (;n<m;)
            {
                cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[0];
                cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[1];
            }
        }

        while (j > 0)
        {
            if (nLength == i)
            {
                break;
            }
            if ((nLength - i) < 256)
            {
                bError = TRUE;
                errRe = 1;
                break;
            }
            file.read(cSecretBuf,256);
            i += 256;
            cSecretBuf[0] ^= cSecretBuf[254];
            cSecretBuf[1] ^= cSecretBuf[255];
            cSecretBuf[0] ^= 0xac;
            cSecretBuf[1] ^= 0xe3;
            if (j < 252)
            {
                m = j + 2;
                nRemainChar = 252 - j;
                j = 0;
                i -= nRemainChar;
            }
            else
            {
                m = 254;
                j -= 252;
                nRemainChar = 0;
            }
            if ((m & 0x01) != 0)
            {
                m--;
                for (n=2;n<m;)
                {
                    cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[0];
                    cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[1];
                }
                cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[0];
            }
            else
            {
                for (n=2;n<m;)
                {
                    cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[0];
                    cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[1];
                }
            }
        }
        j = k;
        k = 0;
        m = j;
        hFile.write(cFileBuf, j);
        if (bError)
            break;
    }

    file.close();
    hFile.close();
    if(errRe == 0)
        return TRUE;
    else
        return FALSE;

    return 0;
}

bool ImportHPGL::ReadSecretFile(QString stePathName)
{
    m_fileFullPathName = stePathName;//文件名称
    QFile file(stePathName);
    unsigned char c;
    unsigned char *cPin;
    int nParameter;
    int nSecretFileLength;
    unsigned char * pSecretFileBuf = NULL;
    QString strTemp;
    int i = 0;//加密指针pSecretFileBuf索引
    VectorFont vectorFont;
    connect(&vectorFont, SIGNAL(siLineTo(bool,QPoint)), this, SLOT(AddTextPointToLine(bool,QPoint)));
    connect(&vectorFont, SIGNAL(siMoveTo(bool,QPoint)), this, SLOT(AddTextPointToLine(bool,QPoint)));
    QPoint ptPoint;
    CRPPolyline RPPolyline;
    CNotch notch;
    CDrill drill;
    int pointx,pointy;

    int notchPenNO = 5;
    int iFontNameLenth = 0;
    int notchOffsetX,notchOffsetY;
    notchOffsetX = notchOffsetY = 0;

    IniPara();

    if (!file.open(QIODevice::ReadOnly))
    {
        return false;
    }

    m_dScale = (double)m_pMarker->m_iDPMM / 40;
    m_listXY.clear();
    nSecretFileLength = file.size();
    pSecretFileBuf = new unsigned char[nSecretFileLength];
    if(pSecretFileBuf != NULL)
    {
        file.read((char*)pSecretFileBuf,nSecretFileLength);
    }
    file.close();

    while (i < nSecretFileLength)
    {
        c = pSecretFileBuf[i++];
        switch (c)
        {
        case DEFCMD_IN://0x8a://IN
            break;
        case DEFCMD_PG://0x97://PG
            m_nLength = m_pMarker->GetRect().right();
            break;
        case DEFCMD_SP://0x7c:
        {
            AddPolylineToMarker();
            unsigned char cTemp;
            cTemp = pSecretFileBuf[i++];
            m_iPenNo = cTemp;
        }
            break;
        case DEFCMD_LT://0xe6:
        {
            c = pSecretFileBuf[i++];
            c ^= 0xce;
            if (c != 0)
            {
                if (c == 1)
                    c = 3;
                else if (c == 2)
                    c = 1;
                else
                    c = 8;
            }
        }
            break;
        case DEFCMD_SI://0x5d: 字体宽×高
        {
            cPin = (unsigned char *)&m_dTextWidth;
            for(int nCount=7; nCount>=0; nCount--)
            {
                *(cPin+nCount) = pSecretFileBuf[i++];
            }
            m_dTextWidth = m_dTextWidth * 10 * M_IDPMM;

            cPin = (unsigned char *)&m_dTextHeight;
            for(int nCount=7; nCount>=0; nCount--)
            {
                *(cPin+nCount) = pSecretFileBuf[i++];
            }
            m_dTextHeight = m_dTextHeight * 10 * M_IDPMM;
        }
            break;
        case DEFCMD_LB://0x4d:
        {
            AddPolylineToMarker();
            cPin = (unsigned char *)&nParameter;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            m_ptCurrentPos.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            m_ptCurrentPos.setY(pointy);

            strTemp.clear();
            QByteArray tempArr;
            tempArr.clear();
            for(int nCount=0; nCount<nParameter; nCount++)
            {
                tempArr.append(pSecretFileBuf[i++]);
            }

            //防止中文乱码
            QTextCodec *gbkCodec = QTextCodec::codecForName("GBK");
            strTemp = gbkCodec->toUnicode(tempArr);

            m_ptCurrentPos = GetTextOrigin(m_ptCurrentPos,strTemp);
            vectorFont.m_dFontHeight = m_dTextHeight;
            vectorFont.m_dFontAngle = m_dTextAngle;
            vectorFont.TextOutString(m_ptCurrentPos.x(),m_ptCurrentPos.y(),(const char*)strTemp.data(),strTemp.length());
        }
            break;
        case DEFCMD_DI://0x5B:
        {
            cPin = (unsigned char *)&m_dTextAngle;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            m_bFontItalic = pSecretFileBuf[i++];

            cPin = (unsigned char *)&m_iFontSize;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
        }
            break;
        case DEFCMD_FN://0x4B:
        {
            m_strFontName = "";
            cPin = (unsigned char *)&iFontNameLenth;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            for (int a = 0; a < iFontNameLenth; a++)
            {
                char chTemp = pSecretFileBuf[i++];
                m_strFontName = m_strFontName + chTemp;
            }
        }
            break;
        case DEFCMD_DRILL://0xD2:冲孔
        {
            AddPolylineToMarker();
            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            drill.m_pt.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            drill.m_pt.setY(pointy);

            cPin = (unsigned char *)&drill.m_nDrillType;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&drill.m_nAngle;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            RPPolyline.m_nDrawingType = 5;
            RPPolyline.m_drill = drill;
            RPPolyline.m_nPenNo = drill.m_nDrillType;
            m_pMarker->m_listPolyline.append(RPPolyline);
        }
            break;
        case DEFCMD_NOTCH://0xD3:剪口
        {
            AddPolylineToMarker();
            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            notch.m_ptStart.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            notch.m_ptStart.setY(pointy);

            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            notch.m_ptEnd.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            notch.m_ptEnd.setY(pointy);

            cPin = (unsigned char *)&notch.m_nWidth;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&notch.m_nNotchType;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            notch.CovertToOutputByOffset(notchOffsetX,notchOffsetY);
            RPPolyline.m_nDrawingType = 6;
            RPPolyline.m_notch = notch;
            RPPolyline.m_nPenNo = notchPenNO;
            m_pMarker->m_listPolyline.append(RPPolyline);
        }
            break;
        case DEFCMD_PU://0xb5:
        case DEFCMD_LPU://0x5e:
        {
            AddPolylineToMarker();
            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            ptPoint.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            ptPoint.setY(pointy);

            m_ptCurrentPos = ptPoint;
            m_bPenUp = true;
        }
            break;
        case DEFCMD_PD://0x3d:
        case DEFCMD_LPD://0x3b:
        {
            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            ptPoint.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            ptPoint.setY(pointy);

            AddPoint2listXY(ptPoint);
            m_ptCurrentPos = ptPoint;
        }
            break;
        case DEFCMD_NULL://0x00:空指令
            AddPolylineToMarker();
            break;
        default:
            qDebug()<<tr("Encrypted file parsing error!");//加密文件解析错误！
            break;
    }
}
    return true;
}

bool ImportHPGL::C_Code()
{
    char cCh1;
    bool bOk;

    bOk=GetChar(&cCh1);
    if (bOk)
    {
        switch (cCh1)
        {
        case 'I':
        case 'i':
            bOk = MoveToNextEnglishChar();
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}

//分析S指令
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 正确的G指令
//  false 不正确的G指令
bool ImportHPGL::S_Code()
{
    char cCh1;
    bool bOk;
    QString strInfo;

    bOk=GetChar(&cCh1);
    if (bOk)  {
        switch (cCh1)  {
        case 'P':
        case 'p':
            AddPolylineToMarker();
            GetIntegerData(m_iPenNo);
            break;
        case 'I':
        case 'i':
            bOk=GetDoubleData(m_dTextWidth);
            m_dTextWidth = m_dTextWidth * 10 * m_iDPMM;
            bOk=SearchChar( ',' ,strInfo);
            bOk=GetDoubleData(m_dTextHeight);
            m_dTextHeight = m_dTextHeight * 10 * m_iDPMM;
            break;
        case 'C':
        case 'c':
            SC_Code();
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}

bool ImportHPGL::SC_Code()
{
    bool bOk;
    QString strInfo;
    double dXMin,dXMax,dYMin,dYMax;
    int nType;

    bOk = GetDoubleData(dXMin);
    bOk=SearchChar(',' ,strInfo);
    bOk = GetDoubleData(dXMax);
    bOk=SearchChar(',' ,strInfo);
    bOk = GetDoubleData(dYMin);
    bOk=SearchChar(',',strInfo);
    bOk = GetDoubleData(dYMax);
    if (NextCharIsComma())
    {
        bOk = GetIntegerData(nType);
        if (NextCharIsComma())
        {
            bOk = GetDoubleData(m_sc.dLeft);
            bOk=SearchChar( ',',strInfo);
            bOk = GetDoubleData(m_sc.dBottom);
        }
    }
    else
    {
        nType = 0;
    }
    if (bOk)
    {
        m_sc.dXMin = dXMin;
        m_sc.dXMax = dXMax;
        m_sc.dYMin = dYMin;
        m_sc.dYMax = dYMax;
        m_sc.nType = nType;
        SetScale();
    }
    bOk = MoveToNextEnglishChar();
    return bOk;
}

bool ImportHPGL::I_Code()
{
    char cCh1;
    bool bOk;
    QString strInfo;

    bOk=GetChar(&cCh1);
    if (bOk)
    {
        switch (cCh1)
        {
        case 'N':
        case 'n':
            break;
        case 'P':
        case 'p':
            int nP1X,nP1Y,nP2X,nP2Y;
            bOk = GetIntegerData(nP1X);
            if (bOk)
            {
                bOk=SearchChar( ',' , strInfo);
                bOk = GetIntegerData(nP1Y);
                bOk=SearchChar(',' ,strInfo);
                bOk = GetIntegerData(nP2X);
                bOk=SearchChar(',' ,strInfo);
                bOk = GetIntegerData(nP2Y);
            }
            if (bOk)
            {
                m_ptP1.setX(nP1X);
                m_ptP1.setY(nP1Y);
                m_ptP2.setX(nP2X);
                m_ptP2.setY(nP2Y);
            }
            bOk = MoveToNextEnglishChar();
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}

bool ImportHPGL::PU_Code()
{
    bool bOk;
    QPoint ptPoint;

    AddPolylineToMarker();
    bOk = GetCoordinate(ptPoint);
    if (bOk)
    {
        m_ptCurrentPos = ptPoint;
    }

    m_bPenUp = true;

    bOk=true;
    return bOk;
}

bool ImportHPGL::PD_Code()
{
    bool bOk;
    QPoint ptPoint;

    bOk = GetCoordinate(ptPoint);
    if (bOk)
    {
        AddPoint2listXY(ptPoint);
        m_ptCurrentPos = ptPoint;
    }
    while (bOk)
    {
        if (NextCharIsComma())
        {
            bOk = GetCoordinate(ptPoint);
            if (bOk)
            {
                AddPoint2listXY(ptPoint);
                m_ptCurrentPos = ptPoint;
            }
        }
        else
        {
            break;
        }
    }
    m_bPenUp = false;

    bOk=true;
    return bOk;
}

bool ImportHPGL::P_Code()
{
    bool bOk;
    char c;

    if (GetChar(&c))
    {
        switch (c)
        {
        case 'U':
        case 'u':
            PU_Code();
            break;
        case 'D':
        case 'd':
            PD_Code();
            break;
        case 'A':
        case 'a':
            if (m_bPenUp)
            {
                PU_Code();
            }
            else
            {
                PD_Code();
            }
            break;
        case 'G':
        case 'g':
            m_bPage = true;
            AddPolylineToMarker();
            m_nLength = m_pMarker->GetRect().right();
            //qDebug()<< "length:" << m_nLength;
            //qDebug()<< "line count:" << m_pMarker->m_listPolyline.size();
            m_bPage = false;
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    bOk=true;
    return bOk;
}

bool ImportHPGL::L_Code()
{
    char cCh1;
    bool bOk;
    QString strText;
    VectorFont vectorFont;
    connect(&vectorFont, SIGNAL(siLineTo(bool,QPoint)), this, SLOT(AddTextPointToLine(bool,QPoint)));
    connect(&vectorFont, SIGNAL(siMoveTo(bool,QPoint)), this, SLOT(AddTextPointToLine(bool,QPoint)));

    CRPPolyline crppolylineTemp;

    bOk=GetChar(&cCh1);
    if (bOk)
    {
        switch (cCh1)
        {
        case 'T':
        case 't':
            AddPolylineToMarker();
            GetLineType(m_lineType);
            bOk = MoveToNextEnglishChar();
            break;
        case 'B':
        case 'b':
            AddPolylineToMarker();
            bOk = SearchChar(m_chTerminator,strText);
            m_ptCurrentPos = GetTextOrigin(m_ptCurrentPos,strText);

            crppolylineTemp.m_nDrawingType = 3;
            crppolylineTemp.m_text.m_strText = strText;
            crppolylineTemp.m_text.m_ptTextPos = m_ptCurrentPos;
            crppolylineTemp.m_text.m_dTextAngle = m_dTextAngle;
            crppolylineTemp.m_text.m_nHeight = m_dTextHeight;
            crppolylineTemp.m_text.m_nWidth  = m_dTextWidth;
            crppolylineTemp.m_text.m_ptPostLU = m_ptCurrentPos;
            crppolylineTemp.m_text.m_ptPostLD = QPoint(m_ptCurrentPos.x(),m_ptCurrentPos.y()+m_dTextHeight);
            crppolylineTemp.m_text.m_ptPostRU = QPoint(m_ptCurrentPos.x()+m_dTextWidth*strText.length(),m_ptCurrentPos.y());
            crppolylineTemp.m_text.m_ptPostRD = QPoint(m_ptCurrentPos.x()+m_dTextWidth*strText.length(),m_ptCurrentPos.y()+m_dTextHeight);

            //用线段方法绘制
#ifdef Q_OS_WIN
            vectorFont.m_dFontHeight = m_dTextHeight;
            vectorFont.m_dFontAngle = m_dTextAngle;
            vectorFont.TextOutString(m_ptCurrentPos.x(),m_ptCurrentPos.y(),strText.toStdString().c_str(),strText.size());
#endif

            //qDebug()<<crppolylineTemp.m_text.m_strText;
            //m_pMarker->m_listPolyline.append(crppolylineTemp);//用文本方法绘制

            break;
        case 'O':
        case 'o':
            bOk=GetIntegerData(m_nLableOrigin);
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}


bool ImportHPGL::D_Code()
{
    char cCh1;
    bool bOk;
    QString strInfo;

    bOk=GetChar(&cCh1);
    if (bOk)  {
        switch (cCh1)  {
        case 'F':
        case 'f':
            break;
        case 'I':
        case 'i':
            double dX,dY;

            bOk = GetDoubleData(dX);
            if (bOk)
            {
                bOk = SearchChar(',' ,strInfo);
                bOk = GetDoubleData(dY);
            }
            else
            {
                dX = 1;
                dY = 0;
            }
            m_dTextAngle = GetTextAngle(dX,dY);
            bOk = MoveToNextEnglishChar();
            break;
        case 'T':
        case 't':
            char chTerminator;
            int nTerminatorMode;
            bOk = GetChar(&chTerminator);
            if (bOk)
            {
                m_chTerminator = chTerminator;
                bOk = GetIntegerData(nTerminatorMode);
                if (bOk)
                {
                    m_nTerminatorMode = nTerminatorMode;
                }
            }
            bOk = MoveToNextEnglishChar();
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}

bool ImportHPGL::Write(QString strPathName,Marker *pMarker)
{
    QFile *writeFile = new QFile(strPathName);
    writeFile->open(QIODevice::ReadWrite | QFile::Truncate);
    // 如果文件没有被占用可以打开
    // 创建stream
    QTextStream txtOutput(writeFile);

    QString strHPGL = "IN;DF;SP0;PU0,0;";
    //int nLength = 0;
    int i,j;
    int iLineCount,iPointCount;

    QString strTemp;
    int nOldPenNo = 0;

    bool bSetLT = false;

    iLineCount = pMarker->m_listPolyline.size();
    for (i = 0; i < iLineCount; i++)
    {
        CRPPolyline polyLine = pMarker->m_listPolyline.at(i);

        if (polyLine.m_lineType.bDefault == false)
        {
            bSetLT = true;
            strTemp = QString("LT%1,%2,%3;").arg(polyLine.m_lineType.nLinetype).arg(polyLine.m_lineType.nPatternLength).arg(1);
        }
        else
        {
            if (bSetLT)
                strTemp = "LT;";
        }
        strHPGL = strHPGL + strTemp;

        if (polyLine.m_nDrawingType == 0)
        {
            iPointCount = polyLine.m_listPoint.size();
            //选择画笔 1为笔绘 3为半透切割 其它为切割
            if (nOldPenNo != polyLine.m_nPenNo)
            {
                if (polyLine.m_nPenNo == 1)
                {
                    strTemp = QString("SP%1;").arg(polyLine.m_nPenNo);
                    nOldPenNo = polyLine.m_nPenNo;
                }
                else if (polyLine.m_nPenNo == 3)
                {
                    strTemp = QString("SP%1;").arg(polyLine.m_nPenNo);
                    nOldPenNo = polyLine.m_nPenNo;
                }
                else
                {
                    strTemp = QString("SP%1;").arg(polyLine.m_nPenNo);
                    nOldPenNo = polyLine.m_nPenNo;
                }
                strHPGL = strHPGL + strTemp;
            }


            for (j = 0; j < iPointCount; j++)
            {

                QPoint pt = polyLine.m_listPoint.at(j);

                if (j == 0)
                {
                    strTemp = QString("PU%1,%2;").arg(pt.x()).arg(pt.y());

                }
                else if(j == 1)
                {
                    if (j + 1 == iPointCount)
                    {
                        strTemp = QString("PD%1,%2;").arg(pt.x()).arg(pt.y());

                    }
                    else
                    {
                        strTemp = QString("PD%1,%2,").arg(pt.x()).arg(pt.y());

                    }
                }
                else
                {
                    if (j + 1 == iPointCount)
                    {

                        strTemp = QString("%1,%2;").arg(pt.x()).arg(pt.y());
                    }
                    else
                    {
                        strTemp = QString("%1,%2,").arg(pt.x()).arg(pt.y());

                    }
                }
                strHPGL = strHPGL + strTemp;
            }
        }
        else if (polyLine.m_nDrawingType == 5)
        {
            strTemp = "SP20;";
            strHPGL = strHPGL + strTemp;
            strTemp = QString("PU%1,%2;").arg(polyLine.m_drill.m_pt.x()).arg(polyLine.m_drill.m_pt.y());
            strHPGL = strHPGL + strTemp;
            strTemp = QString("PD%1,%2;").arg(polyLine.m_drill.m_pt.x()).arg(polyLine.m_drill.m_pt.y());
            strHPGL = strHPGL + strTemp;
            nOldPenNo = 20;
        }

        // 在stream追加数据，并换行
        txtOutput << strHPGL << endl;
        strHPGL = "";

    }
    strHPGL = strHPGL + "SP0;";
    //nLength = strHPGL.size();

    // 在stream追加数据，并换行
    txtOutput << strHPGL << endl;
    strHPGL = "";
    writeFile->close();
    return true;
}

// 生成预览图
int ImportHPGL::createPreviewImage(QImage *pImg, int saveflag, int penwidth, int reDraw)
{
#if(1)
    if(reDraw == 0)//图片存在则不重画
    {
        // 图片是否存在，存在则返回
        QString previewPath = getFileFullPathName() + ".preview.png";
        QFile imageFile(previewPath);
        if (imageFile.exists())
        {
            return 0;
        }
    }
#endif

    int newimgflag = 0;
    QImage * pImage;
    int width, height;
    if (pImg == NULL)
    {
        width = PLT_PREVIEW_WIDTH;
        height = PLT_PREVIEW_HEIGHT;
        pImage = new QImage(width, height, IMAGE_TYPE);
        newimgflag = 1;
    }
    else
    {
        pImage = pImg;
    }

    width = pImage->width();
    height = pImage->height();

    if (width <= PLT_PREVIEW_SIDE*2 || height <= PLT_PREVIEW_SIDE*2)
    {
        if (pImage != NULL && newimgflag == 1)
        {
            delete pImage;
        }
        qDebug("preview img too small");
        return -1;
    }

    memset(pImage->bits(), 0x00, pImage->byteCount());

    QPainter painter(pImage);
    QColor backcolor(255, 255, 255, 0);         // 透明背景

    // 背景
    QPen pen;
    pen.setWidth(penwidth);
    pen.setColor(backcolor);
    painter.setPen(pen);
    painter.setBrush(backcolor);
    painter.drawRect(0, 0, width, height);

    if(m_pMarker == NULL)
    {
        return -1;
    }

    //得到XY边界值
    QRect rect = m_pMarker->GetRect();
    int minX = rect.left();
    int maxX = rect.right();
    int minY = rect.top();
    int maxY = rect.bottom();

    // 图形显示区域
    int dpminx = PLT_PREVIEW_SIDE;
    int dpmaxx = width - PLT_PREVIEW_SIDE;
    int dpminy = PLT_PREVIEW_SIDE + PLT_PREVIEW_SIDE;
    int dpmaxy = height - PLT_PREVIEW_SIDE;

    // 计算缩放系数
    double factor, temp;
    factor = (double)(abs(maxX - minX)) / (dpmaxx - dpminx);      // 按x计算的缩放系数
    temp =   (double)(abs(maxY - minY)) / (dpmaxy - dpminy);      // 按轮廓计算，最小能够放下重复次数个图形
    if (temp >= factor)     // 使用较大的缩放系数
    {
        factor = temp;
    }

    // 计算显示参数，按照图形的实际位置显示（数据坐标零点对应图形中心）
    int dpx = (int)((dpminx+dpmaxx)/2 - ((maxX+minX)/factor)/2);
    int dpy = (int)((dpminy+dpmaxy)/2 - ((maxY+minY)/factor)/2);

    // 显示花样图形
    QColor sewcolor = QColor(Qt::green);
    pen.setColor(sewcolor);
    painter.setPen(pen);

    CBitmapInfo bitmapInfo;
    QPainterPath painterPath;

    int nLineCount = m_pMarker->m_listPolyline.size();
    for(int i = 0; i < nLineCount; i++)
    {
        CRPPolyline polyLine = m_pMarker->m_listPolyline.at(i);
        int type = polyLine.m_nDrawingType;

        if(type == 0)//直线
        {
            int nPointCount = polyLine.m_listPoint.size();
            for(int j = 0; j < nPointCount; j++)
            {
                double x = polyLine.m_listPoint.at(j).x() / factor + dpx;
                double y = polyLine.m_listPoint.at(j).y() / factor + dpy;

                if (PLT_SHOWDIRX == -1)
                {
                    x = width - x;
                }
                if (PLT_SHOWDIRY == -1)
                {
                    y = height - y;
                }

                QPointF point(x,y);

                if(j == 0)
                {
                    painterPath.moveTo(point);
                }
                else
                {
                    painterPath.lineTo(point);
                }
            }
        }
        else if(type == 1)//位图
        {
            bitmapInfo = polyLine.m_bitmapInfo;
            int x = bitmapInfo.m_ptAbPostLU.x();
            int y = bitmapInfo.m_ptAbPostLU.y();

            int nx = x / factor + dpx;
            int ny = y / factor + dpy;

            if (PLT_SHOWDIRX == -1)
            {
                nx = width - nx;
            }
            if (PLT_SHOWDIRY == -1)
            {
                ny = height - ny;
            }

            bitmapInfo.m_ptAbPostLU.setX(nx);
            bitmapInfo.m_ptAbPostLU.setY(ny);
        }
    }

    painter.drawPath(painterPath);
    if(bitmapInfo.m_iBytes > 0)//有位图
    {
        painter.drawPixmap(bitmapInfo.m_ptAbPostLU.x(),bitmapInfo.m_ptAbPostLU.y(),bitmapInfo.m_pBitmap);
    }

    // 保存成文件
    QString preview = getFileFullPathName();
    if (saveflag != 0 && preview.isEmpty() == false)
    {
#if (1)
        preview += ".preview.png";
        pImage->save(preview, "png");
#endif
    }

    if (pImage != NULL && newimgflag == 1)
    {
        delete pImage;
    }

    return 0;
}

//转换为中间绝对坐标数据
void ImportHPGL::convertDataToEmbAbs()
{
    CBitmapInfo bitmapInfo;
    m_embAbsData.clear();

    //得到数据的第一个点
    int firstx, firsty, firstFlag;
    firstx = firsty = firstFlag = 0;
    int nLineCount = m_pMarker->m_listPolyline.size();
    for(int i = 0; i < nLineCount; i++)
    {
        if(firstFlag != 0)
        {
            break;
        }

        CRPPolyline polyLine = m_pMarker->m_listPolyline.at(i);
        int type = polyLine.m_nDrawingType;

        if(type == 0)//直线
        {
            int nPointCount = polyLine.m_listPoint.size();
            for(int j = 0; j < nPointCount; j++)
            {
                firstx = polyLine.m_listPoint.at(j).x();
                firsty = polyLine.m_listPoint.at(j).y();
                firstFlag = 1;
                break;
            }
        }
        else if(type == 1)//位图
        {
            bitmapInfo = polyLine.m_bitmapInfo;
            firstx = bitmapInfo.m_ptAbPostLU.x();
            firsty = bitmapInfo.m_ptAbPostLU.y();
            firstFlag = 1;
        }
    }

    //转换为绝对坐标的中间数据
    QByteArray absData;//绝对坐标
    absData.clear();
    DsAbsItem absItem;
    memset(&absItem,0,sizeof(DsAbsItem));
    absItem.ctrl = m_stepCtrl;
    // 绝对坐标数据
    DataDs16FileHead dhead;
    memset(&dhead, 0, sizeof(DataDs16FileHead));

    double xfactor = (1.0 / (double)M_IDPMM) * PLT_EMB_DATADIRX * PLT_EMB_DATASCALE;            // coco 转换xy方向
    double yfactor = (1.0 / (double)M_IDPMM) * PLT_EMB_DATADIRY * PLT_EMB_DATASCALE;

    if(g_emMacType == MACHINE_DRAWING)
    {
        yfactor *= -1;//正确为1

        if(UNITDEC)
        {
            xfactor /= 10.0;
            yfactor /= 10.0;
        }
    }

    //得到XY边界值
    QRect rect = m_pMarker->GetRect();
    m_minX = (rect.left() - firstx) * xfactor;
    m_maxX = (rect.right() - firstx) * xfactor;
    m_minY = (rect.top() - firsty) * yfactor;
    m_maxY = (rect.bottom() - firsty) * yfactor;

    double x,y;
    x = y = 0;
    int stepidx = 0;
    double ddx, ddy, prex, prey, ar;
    ddx = ddy = ar = 0;
    //前一针初始值
    prex = (0 - firstx) * xfactor;
    prey = (0 - firsty) * yfactor;

    for(int i = 0; i < nLineCount; i++)
    {
        CRPPolyline polyLine = m_pMarker->m_listPolyline.at(i);
        int type = polyLine.m_nDrawingType;

        if(type == 0)//直线
        {
            int nPointCount = polyLine.m_listPoint.size();
            for(int j = 0; j < nPointCount; j++)
            {
                x = (polyLine.m_listPoint.at(j).x() - firstx) * xfactor;
                y = (polyLine.m_listPoint.at(j).y() - firsty) * yfactor;

                ddx = x - prex;
                ddy = y - prey;
                double tar = atan2(ddy,ddx);
                ar = (tar * 10000+0.5*(tar>0?1:-1));


                //图元之间的连接转换为偏移针步
                if(i != 0 && j == 0)
                {
                    absItem.ctrl = DATA_OFFSET;
                }
                else
                {
                    if(g_emMacType == MACHINE_FLATCUTTINGMAC)     //平板切割机
                    {
                        if(polyLine.m_nPenNo == 1){
                            prex = x;
                            prey = y;
                            continue;
                        }
                        else if(polyLine.m_nPenNo == 2){
                            prex = x;
                            prey = y;
                            continue;
                        }
                        else if(polyLine.m_nPenNo == 3)
                            absItem.ctrl = DATA_EKNIFE2;    // DATA_SKNIFE 改为 DATA_EKNIFE2   coco
                        else if(polyLine.m_nPenNo == 4)
                            absItem.ctrl = DATA_EKNIFE1;
                    }
                    else if(g_emMacType == MACHINE_FLYFLATCUTTING)
                    {
                        if(polyLine.m_nPenNo == 1){
                            absItem.ctrl = DATA_DRAWING;//画笔
                        }
                        else if(polyLine.m_nPenNo == 2){
                            prex = x;
                            prey = y;
                            continue;
                        }
                        else if(polyLine.m_nPenNo == 3){
                            absItem.ctrl = DATA_SKNIFE;
                        }
                        else if(polyLine.m_nPenNo == 4)
                        {
                            absItem.ctrl = DATA_RKNIFE;
                        }
                    }
                    else if(g_emMacType == MACHINE_DRAWING)
                    {
                        if(polyLine.m_nPenNo == 1){
                            absItem.ctrl = DATA_DRAWING;//画笔
                        }
                    }
                    else
                    {
                        absItem.ctrl = m_stepCtrl;
                    }

                }

                absItem.ax = x;
                absItem.ay = y;
                absItem.ar = ar;
                prex = x;
                prey = y;

                absData.append((char*)(&absItem), sizeof(DsAbsItem));
                stepidx++;
            }
        }
        else if(type == 1)//位图
        {
            bitmapInfo = polyLine.m_bitmapInfo;
            x = (bitmapInfo.m_ptAbPostLU.x() - firstx) * xfactor;
            y = (bitmapInfo.m_ptAbPostLU.y() - firsty) * yfactor;

            ddx = x - prex;
            ddy = y - prey;
            double tar = atan2(ddy,ddx);
            ar = (tar * 10000+0.5*(tar>0?1:-1));

            //图元之间的连接转换为偏移针步
            absItem.ctrl = DATA_OFFSET;
            absItem.ax = x;
            absItem.ay = y;
            absItem.ar = ar;
            prex = x;
            prey = y;

            absData.append((char*)(&absItem), sizeof(DsAbsItem));
            stepidx++;
        }
    }

    //添加结束针步数据
    absItem.ctrl = 0;
    absData.append((char*)(&absItem), sizeof(DsAbsItem));
    stepidx++;

    int newstepnum = stepidx;    // 转换后的数据长度

    // 文件头转换
    QString name = getFileFullPathName();
    int namelen = name.size();
    if (namelen > HEAD_NAME_STR_LEN)
    {
        namelen = HEAD_NAME_STR_LEN;
    }
    memcpy(dhead.fileName, name.data(), namelen);   // 文件名称
    dhead.dataSize = newstepnum*sizeof(DsAbsItem);    // 数据字节数
    dhead.itemNums = newstepnum;                    // 数据项个数
    dhead.bytesPerItem = sizeof(DsAbsItem);           // 每项占的字节数
    dhead.bytesPerBlk = MAX_EXDP_LEN;               // 数据内容划分块大小
    dhead.dataChecksum = calcCheckSum32((u8 *)(absData.data()) , absData.length());		// 数据累加校验和
    dhead.checkCrc = calcCrc16((u8 *)(&dhead), HEAD_FIX_INFO_LEN);	// 前面6个字段的CRC校验，（6个字段分别为：文件名称，字节数，项个数，每项字节数，每块字节数，数据累加和的CRC校验值）
    u32 fileID = 0;
    fileID = calcCheckSum32((u8 *)(absData.data()) , absData.length());		// 原始数据累加校验和
    dhead.fileid = fileID;  //中间数据的 fileid  =

    dhead.anchorX = 0;           // 定位点坐标X
    dhead.anchorY = 0;           // 定位点坐标Y
    dhead.beginX = 0;             // 数据起点坐标X
    dhead.beginY = 0;             // 数据起点坐标Y
    dhead.beginR = 0;
    dhead.minX = m_minX;
    dhead.maxX = m_maxX;
    dhead.minY = m_minY;
    dhead.maxY = m_maxY;                // 轮廓范围，使用重新计算之后的值

    // 保存文件头到数组中
    m_embAbsData.append((char *)(&dhead), sizeof(DataDs16FileHead));

    // 保存数据到数组中
    m_embAbsData.append(absData);
}

void ImportHPGL::setStepCtrl(u8 ctrl)
{
    m_stepCtrl = ctrl;
}

QByteArray ImportHPGL::getEmbAbsData()
{
    return m_embAbsData;
}

#if(0)
QPainterPath ImportHPGL::GetPolylinePainterPath()
{
    return m_polylinePainterPath;
}
#endif

#if(0)
int ImportHPGL::CreatePreviewImage(QString saveName,QImage *pImg, int saveflag)
{
    QImage * pImage = NULL;
    int width, height;
    width = height = 0;

    if (pImg == NULL)
    {
        width = (m_maxX - m_minX) / M_IDPMM + 2*PREVIEW_SIDE;
        height = (m_maxY - m_minY) / M_IDPMM+ 2*PREVIEW_SIDE;
        pImage = new QImage(width, height, QImage::Format_ARGB32); //
    }
    else
    {
        pImage = pImg;
    }

    width  = pImage->width();
    height = pImage->height();
    if (width < PREVIEW_SIDE*2 || height < PREVIEW_SIDE*2)
    {
        if (pImage != NULL)
        {
            delete pImage;
        }
        return -1;
    }

    QPainter painter(pImage);
    QColor backcolor(255, 255, 255, 255);
    QColor pencolor(0, 0, 0, 255);

    // 背景
    QPen pen;
    pen.setWidth(1);
    pen.setColor(backcolor);
    painter.setPen(pen);
    painter.setBrush(backcolor);
    painter.drawRect(0, 0, width, height);

    // 图形显示区域
    int dpminx = PREVIEW_SIDE;
    int dpmaxx = width - PREVIEW_SIDE;
    int dpminy = PREVIEW_SIDE;
    int dpmaxy = height - PREVIEW_SIDE;

    // 计算缩放系数
    double factor, temp;
    factor = (double)(abs(m_maxX - m_minX)) / (dpmaxx - dpminx);      // 按x计算的缩放系数
    temp =   (double)(abs(m_maxY - m_minY)) / (dpmaxy - dpminy);      // 按轮廓计算，最小能够放下重复次数个图形
    if (temp >= factor)     // 使用较大的缩放系数
    {
        factor = temp;
    }

    // 计算显示参数，按照图形的实际位置显示（数据坐标零点对应图形中心）
    int dpx = (int)((dpminx+dpmaxx)/2 - ((m_maxX+m_minX)/factor)/2);
    int dpy = (int)((dpminy+dpmaxy)/2 - ((m_maxY+m_minY)/factor)/2);

    int curx, cury, prex, prey;
    curx = cury = prex = prey = 0;

    curx = dpx;
    cury = (height) - dpy;

    pen.setColor(pencolor);
    painter.setPen(pen);

    int nLineCount = m_pMarker->m_listPolyline.size();
    for(int i = 0; i < nLineCount; i++)
    {
        CRPPolyline polyLine = m_pMarker->m_listPolyline.at(i);
        int nPointCount = polyLine.m_listPoint.size();
        for(int j = 0; j < nPointCount; j++)
        {
            prex = curx;
            prey = cury;

            curx = polyLine.m_listPoint.at(j).x() / factor+ dpx;
            cury = height - (polyLine.m_listPoint.at(j).y() / factor + dpy);

            if(j == 0)
            {
                continue;
            }

            painter.drawLine(prex, prey, curx, cury);
        }
    }

    // 保存成文件
    if (saveflag != 0)
    {
        saveName += ".preview.bmp";
        pImage->save(saveName, "bmp");
    }

    if (pImage != NULL)
    {
        pImage = NULL;
        delete pImage;
    }

    return 0;
}
#endif
